<?php

namespace app\admin\controller;

use classes\RedisClass;
use think\Controller;
use think\Db;
use think\facade\Cache;
use think\facade\App;

class Clear extends Controller
{

    //清空所有缓存
    public function delCache()
    {

        //清理前同步总票数及浏览量
        $view_count = Cache::store('redis')->get('view_count'); //总浏览数
        $total_vcount = Cache::store('redis')->get('total_vcount'); //总投票量
        Db::name('tp_vote')
            ->where(array('id' => 1))
            ->update([
                'total_vcount' => $total_vcount,
                'view_count' => $view_count,
            ]);

//        if (Cache::clear()) {
        //$path = App::getRootPath() . 'runtime/cache';
        //$this->delFile($path);
//            Cache::store('redis')->handler()->flushAll();
        Cache::store('redis')->handler()->flushDB();
        $this->voteInit();
        $this->success('清理成功');
//        } else {
//            $this->error('清理失败');
//        }
    }

    // 递归删除文件夹
    public function delFile($path, $delDir = FALSE)
    {
        $handle = @opendir($path);
        if ($handle) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..")
                    is_dir("$path/$item") ? $this->delFile("$path/$item", $delDir) : unlink("$path/$item");
            }
            closedir($handle);
            if ($delDir) return rmdir($path);
        } else {
            if (file_exists($path)) {
                return unlink($path);
            } else {
                return FALSE;
            }
        }
    }


    /**
     *投票初始化数据
     */
    public function voteInit()
    {

        $config = RedisClass::get_vote_config_info();
        $lottery = RedisClass::get_lottery_config_info();
        RedisClass::get_banner_info();

        Cache::store('redis')->inc('total_vcount', $config['total_vcount']);
        Cache::store('redis')->inc('view_count', $config['view_count']);

        $today = date("Y-m-d");
        $tomorrow = strtotime(date("Y-m-d", strtotime("+1 day")));
        $list = Db::name('tp_vote_record')->whereTime('touch_time', 'today')->select();
        foreach ($list as $one) {
            Cache::store('redis')->handler()->LPUSH('record:wecha_id:' . $one['wecha_id'] . ':time:' . $today, 1); //队列
            Cache::store('redis')->handler()->expireat('record:wecha_id:' . $one['wecha_id'] . ':time:' . $today, $tomorrow);//设置过期时间
        }


        //用户
//        $memberList = Db::name('tp_member')->select();
//        foreach ($memberList as $one) {
//            Cache::store('redis')->handler()->hMset('member:mobile:' . $one['mobile'], $one);
//        }

        //抽奖机会
        $allLotteryList = Db::name('tp_lottery_num')->field('wecha_id')->select();
        if (!empty($allLotteryList)) {
            foreach ($allLotteryList as $one) {
                Cache::store('redis')->handler()->LPUSH('lottery:wecha_id:' . $one['wecha_id'], 1);
            }
        }

        //项目
        $itemList = Db::name('tp_vote_item')
            ->alias('a')
            ->join('tp_vote_cate b', 'b.cate_id=a.cate_id', 'left')
            ->join('tp_region c', 'c.id=a.province_id', 'left')
            ->join('tp_region d', 'd.id=a.city_id', 'left')
            ->field('a.id,a.item,a.sub_item,a.sub_item2,a.info,a.info2,a.address,a.type,a.cate_id,a.vcount,a.small_pic,a.startpicurl,a.startpicurl2,a.startpicurl3,a.startpicurl4,a.startpicurl5,a.ewmimage,a.videourl,a.intro,a.status,a.wechat_id,a.addtime,a.checktime,a.checkinfo,a.source,b.name cate_name,c.name province_name,d.name city_name')
            ->where('status > 0')
            ->select();
        if (!empty($itemList)) {
            foreach ($itemList as $one) {
                Cache::store('redis')->handler()->hMset('vote:item_id:' . $one['id'], $one);
                Cache::store('redis')->handler()->zAdd('vote:item_ids', $one['id'], $one['id']);//全部项目
                Cache::store('redis')->handler()->zAdd('vote:item_ids' . $one['cate_id'], $one['id'], $one['id']);//分类项目
                Cache::store('redis')->handler()->zAdd('vote:score:ids', $one['vcount'], $one['id']);//全部排名
                Cache::store('redis')->handler()->zAdd('vote:score:ids' . $one['cate_id'], $one['vcount'], $one['id']);//分类排名
            }
        }

        $itemList2 = Db::name('tp_vote_item')
            ->alias('a')
            ->field('a.id,a.wechat_id')
            ->select();
        if (!empty($itemList2)) {
            foreach ($itemList2 as $one) {
                RedisClass::set_redis_item_by_openid($one['wechat_id']); //报名状态
            }
        }

        //红包数量
        $redpacketList = Db::name('tp_redpacket')->where('status', 0)->select();
        if (!empty($redpacketList)) {
            foreach ($redpacketList as $one) {
                Cache::store('redis')->handler()->LPUSH('redpacket:left', $one['id']); //队列
            }
        }

        //劲酒优惠券数量
        /*$couponList = Db::name('tp_coupon')->where('status', 0)->select();
        if (!empty($couponList)) {
            foreach ($couponList as $one) {
                Cache::store('redis')->handler()->LPUSH('jj_coupon:left', $one['code']); //队列
            }
        }*/

        //1,2,3,4等奖剩余数量
        $lottery1count = Db::name('tp_lottery_record')
            ->where('prize', 1)
            ->count();

        if ($lottery['firstnums'] - $lottery1count > 0) {
            for ($i = 1; $i <= ($lottery['firstnums'] - $lottery1count); $i++) {
                Cache::store('redis')->handler()->LPUSH('lottery:prize:1', 1); //队列
            }
        }
        $lottery2count = Db::name('tp_lottery_record')
            ->where('prize', 2)
            ->count();

        if ($lottery['secondnums'] - $lottery2count > 0) {
            for ($i = 1; $i <= ($lottery['secondnums'] - $lottery2count); $i++) {
                Cache::store('redis')->handler()->LPUSH('lottery:prize:2', 1); //队列
            }
        }
        $lottery3count = Db::name('tp_lottery_record')
            ->where('prize', 3)
            ->count();

        if ($lottery['thirdnums'] - $lottery3count > 0) {
            for ($i = 1; $i <= ($lottery['thirdnums'] - $lottery3count); $i++) {
                Cache::store('redis')->handler()->LPUSH('lottery:prize:3', 1); //队列
            }
        }
//        $lottery4count = Db::name('tp_lottery_record')
//            ->where('prize', 4)
//            ->count();
//
//        if ($lottery['fourdaynums'] - $lottery4count > 0) {
//            for ($i = 1; $i <= ($lottery['fourdaynums'] - $lottery4count); $i++) {
//                Cache::store('redis')->handler()->LPUSH('lottery:prize:4', 1); //队列
//            }
//        }

        //1,2,3,4等奖今日已发放的数量
        $lotteryToday = Db::name('tp_lottery_record')
            ->where('prize > 0')
            ->whereTime('time', 'today')
            ->select();

        if (!empty($lotteryToday)) {
            foreach ($lotteryToday as $one) {
                Cache::store('redis')->handler()->LPUSH('lottery:day:prize:' . $one['prize'] . ':time:' . $today, 1); //队列
                Cache::store('redis')->handler()->expireat('lottery:day:prize:' . $one['prize'] . ':time:' . $today, $tomorrow);//设置过期时间
            }
        }
    }

}
